<%
/**
 * third wizard page / tab
 *
 * @author	Jeroen Wesbeek <work@osx.eu>
 * @package AjaxFlow
 */
%>
<script type="text/javascript" >
 function remappingBean(){
	
	    $("#add_more_immobile").click(function(e){
	        e.preventDefault();
	        addImmobile('','','','','','','','','','','');
	    });  
	    <g:if test="${customer?.immobile}">
		    <g:each in="${customer.immobile}" var="item">
		    	<g:if test="${item.immobileType}">
		    	addImmobile('${item.immobileType.id}',${item.metriQuadri},'${item.province?.nome}','${item.province?.id}','${item.municipality?.nome}','${item.municipality?.id}','${item.cap}','${item.address}','${item.stimaMercatoAttuale}','${item.rataMensileMutuo}','${item.valoreMutuo}');	
		    	 </g:if>	
			</g:each>
	    </g:if>	   
    
	    $("#add_more_bene").click(function(e){
	        e.preventDefault();
	        addBene('','','');
	    });	
	    <g:if test="${customer?.altroBene}">
		    <g:each in="${customer.altroBene}" var="item">
		    	<g:if test="${item.tipologyBene}">
		    	addBene('${item.tipologyBene}','${item.valoreStimaMerc}','${item.note}');	
		    	 </g:if>	
			</g:each>
	    </g:if>		    		 
 }	

	    function addImmobile(des,met,pro,pro_id,com,com_id,cap,res,rmm,vmr,sma){
	    	var next = parseInt($("#count_immobile").val());
	        var addto = "#new_row_immobile_" + (next);
	        var divRow=$('<div class="row" id="new_row_immobile_' + (next+1)+'"></div>');
	        var divColBtn=$('<div class="col-sm-1"></div>');
	        var removeButton = $('<button type="button" class="btn btn-danger btn-md " id="remove_bene_' + (next+1) + '" >'+
	    	        				'<i class="glyphicon glyphicon-trash"></i></button>	');
	        removeButton.click(function(e){
                e.preventDefault();
                var fieldNum = this.id.charAt(this.id.length-1);
                var fieldID = "#new_row_immobile_" + fieldNum;
                $(fieldID).remove();
                var next = parseInt($("#count_immobile").val());
                next = (next -1);
                $("#count_immobile").val(next);  
            }); 
	        divColBtn.append(removeButton)
	        divRow.append(divColBtn);

	        var immobileType=$('<div class="col-sm-5"></div>');
	        var ft = $('<div class="form-group"></div>');
	        var selectContainer=$('<div class="input-group" ><span class="input-group-addon"><i class="fa fa-briefcase fa-lg fa-fw">'+
	    	        '</i></span></div>');  
	        var allOption=$('<select id="immobileType" name="immobileType" class="form-control"/>');
	        
	        <g:each in="${com.macrosolution.exdebito.ImmobileType?.list()}" var="a" status="i">
		        var divOption${i}=null
		        if('${a.id}' == des){
		        	divOption${i}=$('<option value="${a.immobileType}" selected >${a.immobileType}</option>');
			    }else{
			        	divOption${i}=$('<option value="${a.immobileType}" >${a.immobileType}</option>');
				}	 
	        	allOption.append(divOption${i});
	        </g:each>
	        
	        selectContainer.append(allOption);
	        ft.append(selectContainer);	
	        immobileType.append(ft);
	        divRow.append(immobileType);
	        
	        var rendValue=$('<div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-external-link fa-lg fa-fw"></i></span>'+
	    	        			'<input class="form-control input-md" placeholder="Metri Quadri" type="text" name="metriQuadri" id="metriQuadri" value="'+((met!=null &&met!=0)?met:'')+'">'+
	    	        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>'+
	    	        			'<input style="width:100%" data-placeholder="Provincia Immobile" class="input-md" name="immobil_province" id="immobil_province_' + (next+1)+'" value="'+((pro!=null)?pro:'')+'">'+
	    	        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>'+
								'<select style="width:100%" data-placeholder="Comune Immobile" class="input-md" name="immobil_municipality" id="immobil_municipality_' + (next+1)+'" value="'+((com!=null)?com:'')+'">'+
								'<option></option></select>'+
			        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>'+
	    	        		'<input class="form-control input-md" placeholder="CAP" type="text" name="immobil_cap" id="immobil_cap" readonly="readonly" value="'+((cap!=null && cap!='0')?cap:'')+'">'+
			        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>'+
								'<input style="width:100%" placeholder="Indirizzo" class="form-control input-md" type="text" name="immobil_address" id="immobil_address" value="'+((res!=null)?res:'')+'">'+
			        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
			        		'<span class="input-group-addon"><i class="fa fa-eur fa-lg fa-fw"></i></span>'+
		        				'<input class="form-control input-md currency" placeholder="Rata Mensile Mutuo" type="text" name="rataMensileMutuo" id="rataMensileMutuo" value="'+((rmm!=null && rmm!=0)?rmm:'')+'">'+
		        			'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
			        		'<span class="input-group-addon"><i class="fa fa-eur fa-lg fa-fw"></i></span>'+
		        				'<input class="form-control input-md currency" placeholder="Valore Mutuo Residuo" type="text" name="valoreMutuo" id="valoreMutuo" value="'+((vmr!=null && vmr!=0)?vmr:'')+'">'+
		        			'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
			        		'<span class="input-group-addon"><i class="fa fa-money fa-lg fa-fw"></i></span>'+
		        				'<input class="form-control input-md currency" placeholder="Stima Mercato Attuale" type="text" name="stimaMercatoAttuale" id="stimaMercatoAttuale" value="'+((sma!=null && sma!=0)?sma:'')+'">'+
		        			'</div></div></div></div>');
	        divRow.append(rendValue);
	        next = next + 1;
	        $(addto).after(divRow);
	        $("#count_immobile").val(next);  
	        
	        $("#immobil_province_" + (next)+"").select2({
                allowClear: true,
                minimumInputLength: 1,
                formatNoMatches:'Nessuna corripondenza',
                formatInputTooShort:'Inserire almeno un carattere per iniziare la ricerca',
                formatSearching:'Stiamo cercando le province...' ,
                
                ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: "/province/list",
                    dataType: 'json',
                    quietMillis: 250,
                    data: function (term, page) {
                        return {
                            index: term, // search term
                        };
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter the remote JSON data
                        return { results: data.results };
                    },
                    
                    cache: true
                },   
             });   
	      //BENI E PROPRIETA STEP
            $("#immobil_province_" + (next)+"").on("change", function(e) {
            	jQuery.ajax({type:'POST',data:'pr=' + this.value, url:'/province/get_municipality',
                			success:function(data,textStatus){jQuery('#immobil_municipality_'+next+'').html(data);$("#immobil_municipality_" + (next)+"").select2("val","");$("#immobil_cap").val("");},
                			error:function(XMLHttpRequest,textStatus,errorThrown){}}); 
			}); 
            if(pro_id !=''){
            	$("#immobil_province_" + (next)+"").select2('data',{id:pro_id,text:''+pro});

                }
              
            $('#immobil_municipality_'+(next)+'').select2({
             	allowClear: false,
                 formatNoMatches:'Nessuna corripondenza',
                 formatInputTooShort:'Inserire almeno un carattere per iniziare la ricerca',
                 formatSearching:'Stiamo cercando i comuni...' ,
              });
            $("#immobil_municipality_" + (next)+"").on("change", function(e) {
            	jQuery.ajax({type:'POST',data:'mun=' + this.value, url:'/province/get_cap',
                			success:function(data){$('#immobil_cap').val(data);},
                			error:function(XMLHttpRequest,textStatus,errorThrown){}}); 
			});             
            if(com_id!=''){
            	$("#immobil_municipality_"+(next)+"").append($('<option>', {value:com_id, text: com}));
            	$("#immobil_municipality_"+(next)+"").select2("val",com_id);
                }

            currencyFormatter();
	    }

	function addBene(tip,vsm,not){
	    	var next = parseInt($("#count_bene").val());
	        var addto = "#new_row_bene_" + (next);
	        var divRow=$('<div class="row" id="new_row_bene_' + (next+1)+'"></div>');
	        var divColBtn=$('<div class="col-sm-1"></div>');
	        var removeButton = $('<button type="button" class="btn btn-danger btn-md " id="remove_bene_' + (next+1) + '" >'+
	    	        				'<i class="glyphicon glyphicon-trash"></i></button>	');
	        removeButton.click(function(e){
                e.preventDefault();
                var fieldNum = this.id.charAt(this.id.length-1);
                var fieldID = "#new_row_bene_" + fieldNum;
                $(fieldID).remove();
                var next = parseInt($("#count_bene").val());
                next = (next -1);
                $("#count_bene").val(next);  
            }); 
	        divColBtn.append(removeButton)
	        divRow.append(divColBtn);
	        var rendValue=$('<div class="col-sm-5 "><div class="form-group"><div class="input-group"><span class="input-group-addon">'+
	    	        		'<i class="fa fa-info fa-lg fa-fw"></i></span>'+
	    	        			'<input class="form-control input-md" placeholder="Tipologia bene" type="text" name="tipologyBene" id="tipologyBene" value="'+((tip!=null)?tip:'')+'">'+
	    	        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-money fa-lg fa-fw"></i></span>'+
	    	        			'<input class="form-control input-md currency" placeholder="Valore/Stima Mercato" type="text" name="valoreStimaMerc" id="valoreStimaMerc" value="'+((vsm!=null)?vsm:'')+'">'+
	    	        		'</div></div></div><div class="col-sm-5 col-md-offset-1"><div class="form-group"><div class="input-group">'+
	    	        		'<span class="input-group-addon"><i class="fa fa-edit fa-lg fa-fw"></i></span>'+
	    	        			'<input class="form-control input-md" placeholder="Note" type="text" name="note" id="note" value="'+((not!=null)?not:'')+'">'+
	    	        		'</div></div></div></div>');
	        divRow.append(rendValue);
	        next = next + 1;
	        $(addto).after(divRow);
	        $("#count_bene").val(next);  
	        currencyFormatter();    
	}
	    
var interviewId = ${interview?.id };
</script>

<af:page>
	<div class="alert alert-danger fade in" id="validity_label"></div>
	<g:if test="${interview}">
	<div class="col-sm-3 col-md-offset-9">
		<h3>Intervista : <exdutility:number_interview value="${interview?.id }"/></h3>
	</div>
</g:if>


	<h3>Inserire Immobile di Proprieta&nbsp;&nbsp;&nbsp;
		<a class="collapsed">  
			<button type="button" class="btn btn-info btn-md " id="add_more_immobile" style="margin-bottom:12px">
				<i class="fa fa-plus-circle"></i>
			</button>
			
		</a>
	</h3>
<input type="hidden" name="count_immobile" id="count_immobile" value="0" />
<div id="new_row_immobile_0"></div>
</br></br>


	<h3>Inserire Altro Bene di Proprieta&nbsp;&nbsp;&nbsp;
		<a class="collapsed">  
			<button type="button" class="btn btn-info btn-md " id="add_more_bene" style="margin-bottom:12px">
				<i class="fa fa-plus-circle"></i>
			</button>
			
		</a>
	</h3>


<input type="hidden" name="count_bene" id="count_bene" value="0" />
<div id="new_row_bene_0"></div>

</h3>

</br></br></br>
<div style="border-top: 2px solid #006DBA; padding-top: 2px;"></div>
</af:page>
